package com.zxkj.commonpatrol.ui;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;

import com.czy.basecommon.utils.base.BaseVmDataBindingActivity;
import com.czy.basecommon.utils.tools.ActivityTool;
import com.jeremyliao.liveeventbus.LiveEventBus;
import com.zxkj.commonpatrol.BR;
import com.zxkj.commonpatrol.R;
import com.zxkj.commonpatrol.adapt.MyTextSimpleAdapter;
import com.zxkj.commonpatrol.databinding.ActivityProblemListBinding;
import com.zxkj.commonpatrol.model.entity.Group;

import com.zxkj.commonpatrol.viewmodel.ProblemListViewModel;

import java.util.ArrayList;
import java.util.List;

public class ProblemListActivity extends BaseVmDataBindingActivity<ActivityProblemListBinding, ProblemListViewModel> {
    private int m=0;
    private int size=0;
    private MyTextSimpleAdapter simpleAdapter;

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_problem_list;
    }

    @Override
    public int initVariableId() {
        return BR.mPatrolListViewModel;
    }

    @Override
    protected void initUI() {
        LinearLayoutManager linearLayoutManager=new LinearLayoutManager(this);
        binding.recyProblem.setLayoutManager(linearLayoutManager);

        binding.btnYes.setClickable(false);
        binding.btnYes.setEnabled(false);
        binding.btnYes.setBackgroundColor(Color.parseColor("#D2D2D2"));

        binding.btnNo.setClickable(false);
        binding.btnNo.setEnabled(false);
        binding.btnNo.setBackgroundColor(Color.parseColor("#D2D2D2"));
    }

    @Override
    protected void setListener() {
        super.setListener();
        binding.btnYes.setOnClickListener(v->{
            if (m<1){
                binding.btnNo.setClickable(false);
                binding.btnNo.setEnabled(false);
                binding.btnNo.setBackgroundColor(Color.parseColor("#D2D2D2"));
                Bundle bundle =new Bundle();
                bundle.putBoolean("a",true);
                startActivity(NewExceptionActivity.class,bundle);
            }else if (m==1){
                binding.btnNo.setClickable(false);
                binding.btnNo.setEnabled(false);
                binding.btnNo.setBackgroundColor(Color.parseColor("#D2D2D2"));
                Bundle bundle =new Bundle();
                bundle.putBoolean("a",false);
                startActivity(NewExceptionActivity.class,bundle);
            }else{
                binding.btnNo.setClickable(false);
                binding.btnNo.setEnabled(false);
                binding.btnNo.setBackgroundColor(Color.parseColor("#D2D2D2"));
                Bundle bundle =new Bundle();
                bundle.putString("add","1");
                startActivity(NewExceptionActivity.class,bundle);
            }

        });

        binding.btnComit.setOnClickListener(v->{
            if (m==1){
                binding.btnYes.setClickable(true);
                binding.btnYes.setEnabled(true);
                binding.btnYes.setBackgroundColor(getColor(R.color.login_btn));

                binding.btnNo.setClickable(true);
                binding.btnNo.setEnabled(true);
                binding.btnNo.setBackgroundColor(getColor(R.color.login_btn));
                List<Group> groups=new ArrayList<>();
                binding.btnComit.setVisibility(View.GONE);
                binding.tvProblem.setText("有沒有其他問題");
                simpleAdapter =new MyTextSimpleAdapter( binding.recyProblem,groups,this);
                binding.recyProblem.setAdapter(simpleAdapter);
                m++;
            }else if (m<1){
                binding.btnYes.setClickable(false);
                binding.btnYes.setEnabled(false);
                binding.btnYes.setBackgroundColor(Color.parseColor("#D2D2D2"));
                binding.btnComit.setVisibility(View.GONE);
                binding.btnNo.setClickable(false);
                binding.btnNo.setEnabled(false);
                binding.btnNo.setBackgroundColor(Color.parseColor("#D2D2D2"));
                List<Group> groups=new ArrayList<>();
                binding.tvProblem.setText("火警報警系統是否有問題");
                Group group =new Group();
                group.setTitle("火警報警系統不正常");
                group.setTYPE(1);
                groups.add(group);
                simpleAdapter =new MyTextSimpleAdapter( binding.recyProblem,groups,this);
                binding.recyProblem.setAdapter(simpleAdapter);
                m++;

            }else if (m>1){
                LiveEventBus.get("finish").post(true);
                ActivityTool.getInstance().popOneActivity(this);
            }
        });
    }

    @Override
    protected void initData() {
        List<Group> groups=new ArrayList<>();

        Group group =new Group();

        group.setTitle("冷氣空調開關不正常");
        group.setTYPE(1);
        groups.add(group);
        simpleAdapter =new MyTextSimpleAdapter( binding.recyProblem,groups,this);
        binding.recyProblem.setAdapter(simpleAdapter);
    }

    @Override
    protected void dataObserver() {
        super.dataObserver();

        LiveEventBus.get("isClick",Boolean.class).observe(this, new Observer<Boolean>() {
            @Override
            public void onChanged(Boolean aBoolean) {
                if (aBoolean){

                    binding.btnYes.setClickable(true);
                    binding.btnYes.setEnabled(true);
                    binding.btnYes.setBackgroundColor(getColor(R.color.login_btn));

                    binding.btnNo.setClickable(true);
                    binding.btnNo.setEnabled(true);
                    binding.btnNo.setBackgroundColor(getColor(R.color.login_btn));
                }
            }
        });

        LiveEventBus.get("cs", Group.class).observe(this, new Observer<Group>() {
            @Override
            public void onChanged(Group s) {
                List<Group> groups = new ArrayList<>();
                groups.add(s);
                simpleAdapter.addData(0,groups.get(0));
                binding.btnComit.setVisibility(View.VISIBLE);
            }
        });



    }
}
